﻿#region Legal notice
/*
 * This file is part of EPPLib.
 * Copyright 2010, 2011 
 * Gianluca Esposito (info@gianlucaesposito.it)
 * ----------------------------------------------------------------------------
 * EPPLib is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 * 
 * EPPLib is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with Nome-Programma; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */
#endregion

#region Using directives
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using EPPLib.Data;
#endregion

namespace EPPLib.Managers
{
	public class DataManager : IDataManager
	{
		#region Constants
		private const string CONNECTION_STRING_NAME = "DataConnectionString";

		private const string CONTACT_GETALL_PROCEDURE = "epp_Contacts_GetAll";
		private const string CONTACT_GETBYID_PROCEDURE = "epp_Contacts_GetByID";
		private const string CONTACT_DELETE_PROCEDURE = "epp_Contacts_Delete";
		private const string CONTACT_SAVE_PROCEDURE = "epp_Contacts_Save";

        private const string DOMAIN_GETALL_PROCEDURE = "epp_Domains_GetAll";
        private const string DOMAIN_GETBYNAME_PROCEDURE = "epp_Domains_GetByID";
        private const string DOMAIN_DELETE_PROCEDURE = "epp_Domains_Delete";
        private const string DOMAIN_SAVE_PROCEDURE = "epp_Domains_Save";

		private const string CONFIGURATION_GET_PROCEDURE = "epp_Configurations_Get";
		private const string CONFIGURATION_INCREASECOUNT_PROCEDURE = "epp_Configurations_IncreaseOperationCount";
		private const string CONFIGURATION_SAVE_NEWPASSWORD_PROCEDURE = "epp_Configurations_SaveNewPassword";

		private const string COUNTRIES_GETALL_PROCEDURE = "epp_Countries_GetAll";
		private const string COUNTRIES_GETBYCODE_PROCEDURE = "epp_Countries_GetByCode";

		private const string ENTITYTYPES_GETALL_PROCEDURE = "epp_EntityTypes_GetAll";

		private const string TRANSACTIONLOGS_GETBYID_PROCEDURE = "epp_TransactionLogs_GetByID";
		private const string TRANSACTIONLOGS_SAVE_PROCEDURE = "epp_TransactionLogs_Save";
		private const string TRANSACTIONLOGS_GETALL_PROCEDURE = "epp_TransactionLogs_GetAll";
		private const string TRANSACTIONLOGS_CLEAR_PROCEDURE = "epp_TransactionLogs_Clear";

		private const string HOSTS_SAVE_PROCEDURE = "epp_Hosts_Save";
		private const string HOSTS_CLEAR_PROCEDURE = "epp_Hosts_Clear";
		private const string HOSTS_READ_PROCEDURE = "epp_Hosts_Read";
		#endregion

		#region Public methods
		public ContactDataCollection ReadContacts(Guid ownerID)
		{
			ContactDataCollection retCol = new ContactDataCollection();
			using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings[CONNECTION_STRING_NAME].ConnectionString))
			{
				SqlCommand command = new SqlCommand();
				command.CommandText = CONTACT_GETALL_PROCEDURE;
				command.CommandType = CommandType.StoredProcedure;
				command.Connection = connection;
				command.Parameters.Clear();
				command.Parameters.Add("@OwnerID", SqlDbType.UniqueIdentifier).Value = ownerID;
				connection.Open();
				SqlDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection);
				while (reader.Read())
				{
					ContactDataObject retObj = new ContactDataObject();
					ReadContactDataObject(ref reader, ref retObj);
					if (retObj.InternalID > 0) retCol.Add(retObj);
				}
			}
			return retCol;
		}
		public ContactDataObject ReadContact(string id, Guid ownerID)
		{
			ContactDataObject retObj = new ContactDataObject();
			using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings[CONNECTION_STRING_NAME].ConnectionString))
			{
				SqlCommand command = new SqlCommand();
				command.CommandText = CONTACT_GETBYID_PROCEDURE;
				command.CommandType = CommandType.StoredProcedure;
				command.Connection = connection;
				command.Parameters.Clear();
				command.Parameters.Add("@ID", SqlDbType.NVarChar, 32).Value = id;
				command.Parameters.Add("@OwnerID", SqlDbType.UniqueIdentifier).Value = ownerID;
				connection.Open();
				SqlDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection);
				if (reader.Read())
				{
					ReadContactDataObject(ref reader, ref retObj);
				}
			}
			return retObj;
		}
		public ContactDataObject SaveContact(ContactDataObject obj)
		{
			using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings[CONNECTION_STRING_NAME].ConnectionString))
			{
				SqlCommand command = new SqlCommand();
				command.CommandText = CONTACT_SAVE_PROCEDURE;
				command.CommandType = CommandType.StoredProcedure;
				command.Connection = connection;
				command.Parameters.Clear();
				SetContactDataObjectParams(ref command, obj);
				connection.Open();
				command.ExecuteNonQuery();
				obj.InternalID = (int)command.Parameters["@InternalID"].Value;
			}
			return obj;
		}
		public void DeleteContact(string id, Guid ownerID)
		{
			using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings[CONNECTION_STRING_NAME].ConnectionString))
			{
				SqlCommand command = new SqlCommand();
				command.CommandText = CONTACT_DELETE_PROCEDURE;
				command.CommandType = CommandType.StoredProcedure;
				command.Connection = connection;
				command.Parameters.Clear();
				command.Parameters.Add("@ID", SqlDbType.NVarChar, 32).Value = id;
				command.Parameters.Add("@OwnerID", SqlDbType.UniqueIdentifier).Value = ownerID;
				connection.Open();
				command.ExecuteNonQuery();
			}
		}

        public DomainDataCollection ReadDomains(Guid ownerID)
        {
            DomainDataCollection retCol = new DomainDataCollection();
            using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings[CONNECTION_STRING_NAME].ConnectionString))
            {
                SqlCommand command = new SqlCommand();
                command.CommandText = DOMAIN_GETALL_PROCEDURE;
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = connection;
                command.Parameters.Clear();
                command.Parameters.Add("@OwnerID", SqlDbType.UniqueIdentifier).Value = ownerID;
                connection.Open();
                SqlDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection);
                while (reader.Read())
                {
                    DomainDataObject retObj = new DomainDataObject();
                    ReadDomainDataObject(ref reader, ref retObj);
                    if (retObj.InternalID > 0) retCol.Add(retObj);
                }
            }
            return retCol;
        }
        public DomainDataObject ReadDomain(string name, Guid ownerID)
        {
            DomainDataObject retObj = new DomainDataObject();
            using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings[CONNECTION_STRING_NAME].ConnectionString))
            {
                SqlCommand command = new SqlCommand();
                command.CommandText = DOMAIN_GETBYNAME_PROCEDURE;
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = connection;
                command.Parameters.Clear();
                command.Parameters.Add("@Name", SqlDbType.NVarChar, 255).Value = name;
                command.Parameters.Add("@OwnerID", SqlDbType.UniqueIdentifier).Value = ownerID;
                connection.Open();
                SqlDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection);
                if (reader.Read())
                {
                    ReadDomainDataObject(ref reader, ref retObj);
                }
            }
            return retObj;
        }
        public DomainDataObject SaveDomain(DomainDataObject obj)
        {
            using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings[CONNECTION_STRING_NAME].ConnectionString))
            {
                SqlCommand command = new SqlCommand();
                command.CommandText = DOMAIN_SAVE_PROCEDURE;
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = connection;
                command.Parameters.Clear();
                SetDomainDataObjectParams(ref command, obj);

                ClearHosts(obj.Name);
                foreach (HostDataObject host in obj.Hosts)
                {
                    host.DomainName = obj.Name;
                    SaveHost(host);
                }

                connection.Open();
                command.ExecuteNonQuery();
                obj.InternalID = (int)command.Parameters["@InternalID"].Value;
            }
            return obj;
        }
        public void DeleteDomain(string name, Guid ownerID)
        {
            using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings[CONNECTION_STRING_NAME].ConnectionString))
            {
                SqlCommand command = new SqlCommand();
                command.CommandText = DOMAIN_DELETE_PROCEDURE;
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = connection;
                command.Parameters.Clear();
                command.Parameters.Add("@Name", SqlDbType.NVarChar, 255).Value = name;
                command.Parameters.Add("@OwnerID", SqlDbType.UniqueIdentifier).Value = ownerID;
                connection.Open();
                command.ExecuteNonQuery();
            }
        }

		public int SaveHost(HostDataObject obj)
		{
			int retVal = 0;
			using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings[CONNECTION_STRING_NAME].ConnectionString))
			{
				SqlCommand command = new SqlCommand();
				command.CommandText = HOSTS_SAVE_PROCEDURE;
				command.CommandType = CommandType.StoredProcedure;
				command.Connection = connection;
				command.Parameters.Clear();
				command.Parameters.Add("@InternalID", SqlDbType.Int);
				command.Parameters["@InternalID"].Direction = ParameterDirection.Output;
				SetHostDataObjectParams(ref command, obj);
				connection.Open();
				command.ExecuteNonQuery();
				int.TryParse(command.Parameters["@InternalID"].Value.ToString(), out retVal);
			}
			return retVal;
		}
		public HostDataCollection ReadHosts(string domainName)
		{
			HostDataCollection retCol = new HostDataCollection();
			using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings[CONNECTION_STRING_NAME].ConnectionString))
			{
				SqlCommand command = new SqlCommand();
				command.CommandText = HOSTS_READ_PROCEDURE;
				command.CommandType = CommandType.StoredProcedure;
				command.Connection = connection;
				command.Parameters.Clear();
				command.Parameters.Add("@DomainName", SqlDbType.NVarChar, 255).Value = domainName;
				connection.Open();
				SqlDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection);
				while (reader.Read())
				{
					HostDataObject retObj = new HostDataObject();
					ReadHostDataObject(ref reader, ref retObj);
					if (retObj.InternalID > 0) retCol.Add(retObj);
				}
			}
			return retCol;
		}

		public int SaveTransactionLog(TransactionLogDataObject obj)
		{
			int retVal = 0;
			using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings[CONNECTION_STRING_NAME].ConnectionString))
			{
				SqlCommand command = new SqlCommand();
				command.CommandText = TRANSACTIONLOGS_SAVE_PROCEDURE;
				command.CommandType = CommandType.StoredProcedure;
				command.Connection = connection;
				command.Parameters.Clear();
				command.Parameters.Add("@InternalID", SqlDbType.Int);
				command.Parameters["@InternalID"].Direction = ParameterDirection.Output;
				SetTransactionLogDataObjectParams(ref command, obj);
				connection.Open();
				command.ExecuteNonQuery();
				int.TryParse(command.Parameters["@InternalID"].Value.ToString(), out retVal);
			}
			return retVal;
		}

		public void ClearHosts(string domainName)
		{
			using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings[CONNECTION_STRING_NAME].ConnectionString))
			{
				SqlCommand command = new SqlCommand();
				command.CommandText = HOSTS_CLEAR_PROCEDURE;
				command.CommandType = CommandType.StoredProcedure;
				command.Connection = connection;
				command.Parameters.Clear();
				command.Parameters.Add("@DomainName", SqlDbType.NVarChar, 256).Value = domainName;
				connection.Open();
				command.ExecuteNonQuery();
			}
		}

		public TransactionLogDataCollection ReadTransactionLogs(Guid ownerID)
		{
			TransactionLogDataCollection retCol = new TransactionLogDataCollection();
			using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings[CONNECTION_STRING_NAME].ConnectionString))
			{
				SqlCommand command = new SqlCommand();
				command.CommandText = TRANSACTIONLOGS_GETALL_PROCEDURE;
				command.CommandType = CommandType.StoredProcedure;
				command.Connection = connection;
				command.Parameters.Clear();
				command.Parameters.Add("@OwnerID", SqlDbType.UniqueIdentifier).Value = ownerID;
				connection.Open();
				SqlDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection);
				while (reader.Read())
				{
					TransactionLogDataObject retObj = new TransactionLogDataObject();
					ReadTransactionLogDataObject(ref reader, ref retObj);
					if (retObj.InternalID > 0) retCol.Add(retObj);
				}
			}
			return retCol;
		}
		public TransactionLogDataObject ReadTransactionLog(int id, Guid ownerID)
		{
			TransactionLogDataObject retObj = new TransactionLogDataObject();
			using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings[CONNECTION_STRING_NAME].ConnectionString))
			{
				SqlCommand command = new SqlCommand();
				command.CommandText = TRANSACTIONLOGS_GETBYID_PROCEDURE;
				command.CommandType = CommandType.StoredProcedure;
				command.Connection = connection;
				command.Parameters.Clear();
				command.Parameters.Add("@ID", SqlDbType.Int).Value = id;
				command.Parameters.Add("@OwnerID", SqlDbType.UniqueIdentifier).Value = ownerID;
				connection.Open();
				SqlDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection);
				if (reader.Read())
				{
					ReadTransactionLogDataObject(ref reader, ref retObj);
				}
			}
			return retObj;
		}
		public void ClearTransactionLogs(Guid ownerID)
		{
			using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings[CONNECTION_STRING_NAME].ConnectionString))
			{
				SqlCommand command = new SqlCommand();
				command.CommandText = TRANSACTIONLOGS_CLEAR_PROCEDURE;
				command.CommandType = CommandType.StoredProcedure;
				command.Connection = connection;
				command.Parameters.Clear();
				command.Parameters.Add("@OwnerID", SqlDbType.UniqueIdentifier).Value = ownerID;
				connection.Open();
				command.ExecuteNonQuery();
			}
		}
		public EntityTypeDataCollection ReadEntityTypes()
		{
			EntityTypeDataCollection retCol = new EntityTypeDataCollection();
			using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings[CONNECTION_STRING_NAME].ConnectionString))
			{
				SqlCommand command = new SqlCommand();
				command.CommandText = ENTITYTYPES_GETALL_PROCEDURE;
				command.CommandType = CommandType.StoredProcedure;
				command.Connection = connection;
				command.Parameters.Clear();
				connection.Open();
				SqlDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection);
				while (reader.Read())
				{
					EntityTypeDataObject retObj = new EntityTypeDataObject();
					ReadEntityTypeDataObject(ref reader, ref retObj);
					if (retObj.InternalID > 0) retCol.Add(retObj);
				}
			}
			return retCol;
		}
		public CountryDataCollection ReadCountries()
		{
			CountryDataCollection retCol = new CountryDataCollection();
			using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings[CONNECTION_STRING_NAME].ConnectionString))
			{
				SqlCommand command = new SqlCommand();
				command.CommandText = COUNTRIES_GETALL_PROCEDURE;
				command.CommandType = CommandType.StoredProcedure;
				command.Connection = connection;
				command.Parameters.Clear();
				connection.Open();
				SqlDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection);
				while (reader.Read())
				{
					CountryDataObject retObj = new CountryDataObject();
					ReadCountryDataObject(ref reader, ref retObj);
					if (!string.IsNullOrEmpty(retObj.Code)) retCol.Add(retObj);
				}
			}
			return retCol;
		}
		public CountryDataObject ReadCountry(string code)
		{
			CountryDataObject retObj = new CountryDataObject();
			using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings[CONNECTION_STRING_NAME].ConnectionString))
			{
				SqlCommand command = new SqlCommand();
				command.CommandText = COUNTRIES_GETBYCODE_PROCEDURE;
				command.CommandType = CommandType.StoredProcedure;
				command.Connection = connection;
				command.Parameters.Clear();
				command.Parameters.Add("@code", SqlDbType.NVarChar, 16).Value = code;
				connection.Open();
				SqlDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection);
				if (reader.Read())
				{
					ReadCountryDataObject(ref reader, ref retObj);
				}
			}
			return retObj;
		}
		public ConfigurationDataObject ReadConfiguration(Guid userID)
		{
			ConfigurationDataObject retObj = new ConfigurationDataObject();
			using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings[CONNECTION_STRING_NAME].ConnectionString))
			{
				SqlCommand command = new SqlCommand();
				command.CommandText = CONFIGURATION_GET_PROCEDURE;
				command.CommandType = CommandType.StoredProcedure;
				command.Connection = connection;
				command.Parameters.Clear();
				command.Parameters.Add("@UserID", SqlDbType.UniqueIdentifier).Value = userID;
				connection.Open();
				SqlDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection);
				if (reader.Read())
				{
					ReadConfigurationDataObject(ref reader, ref retObj);
				}
			}
			return retObj;
		}
		public void SaveConfigurationNewPassword(Guid userID, string newPassword)
		{
			using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings[CONNECTION_STRING_NAME].ConnectionString))
			{
				SqlCommand command = new SqlCommand();
				command.CommandText = CONFIGURATION_SAVE_NEWPASSWORD_PROCEDURE;
				command.CommandType = CommandType.StoredProcedure;
				command.Connection = connection;
				command.Parameters.Clear();
				command.Parameters.Add("@UserID", SqlDbType.UniqueIdentifier).Value = userID;
				command.Parameters.Add("@NewPassword", SqlDbType.NVarChar, 255).Value = newPassword;
				connection.Open();
				command.ExecuteNonQuery();
			}
		}
		public int IncreaseOperationCount(Guid userID)
		{
			int retVal = 0;
			using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings[CONNECTION_STRING_NAME].ConnectionString))
			{
				SqlCommand command = new SqlCommand();
				command.CommandText = CONFIGURATION_INCREASECOUNT_PROCEDURE;
				command.CommandType = CommandType.StoredProcedure;
				command.Connection = connection;
				command.Parameters.Clear();
				command.Parameters.Add("@Count", SqlDbType.Int);
				command.Parameters["@Count"].Direction = ParameterDirection.Output;
				command.Parameters.Add("@UserID", SqlDbType.UniqueIdentifier).Value = userID;
				connection.Open();
				command.ExecuteNonQuery();
				int.TryParse(command.Parameters["@Count"].Value.ToString(), out retVal);
			}
			return retVal;
		}
		#endregion

		#region Private methods
		private void ReadContactDataObject(ref SqlDataReader reader, ref ContactDataObject obj)
		{
			if (reader["OwnerID"] != null && reader["OwnerID"] != DBNull.Value) obj.OwnerID = (Guid)reader["OwnerID"];
			if (reader["InternalID"] != null && reader["InternalID"] != DBNull.Value) obj.InternalID = (int)reader["InternalID"];
			if (reader["ID"] != null && reader["ID"] != DBNull.Value) obj.ID = (string)reader["ID"];
			if (reader["Name"] != null && reader["Name"] != DBNull.Value) obj.Name = (string)reader["Name"];
			if (reader["Organization"] != null && reader["Organization"] != DBNull.Value) obj.Organization = (string)reader["Organization"];
			if (reader["Street1"] != null && reader["Street1"] != DBNull.Value) obj.Street1 = (string)reader["Street1"];
			if (reader["Street2"] != null && reader["Street2"] != DBNull.Value) obj.Street2 = (string)reader["Street2"];
			if (reader["Street3"] != null && reader["Street3"] != DBNull.Value) obj.Street3 = (string)reader["Street3"];
			if (reader["City"] != null && reader["City"] != DBNull.Value) obj.City = (string)reader["City"];
			if (reader["District"] != null && reader["District"] != DBNull.Value) obj.District = (string)reader["District"];
			if (reader["PostalCode"] != null && reader["PostalCode"] != DBNull.Value) obj.PostalCode = (string)reader["PostalCode"];
			if (reader["CountryCode"] != null && reader["CountryCode"] != DBNull.Value) obj.CountryCode = (string)reader["CountryCode"];
			if (reader["PhonePrefix"] != null && reader["PhonePrefix"] != DBNull.Value) obj.PhonePrefix = (string)reader["PhonePrefix"];
			if (reader["Phone"] != null && reader["Phone"] != DBNull.Value) obj.Phone = (string)reader["Phone"];
			if (reader["PhoneInternal"] != null && reader["PhoneInternal"] != DBNull.Value) obj.PhoneInternal = (string)reader["PhoneInternal"];
			if (reader["FaxPrefix"] != null && reader["FaxPrefix"] != DBNull.Value) obj.FaxPrefix = (string)reader["FaxPrefix"];
			if (reader["Fax"] != null && reader["Fax"] != DBNull.Value) obj.Fax = (string)reader["Fax"];
			if (reader["FaxInternal"] != null && reader["FaxInternal"] != DBNull.Value) obj.FaxInternal = (string)reader["FaxInternal"];
			if (reader["Email"] != null && reader["Email"] != DBNull.Value) obj.Email = (string)reader["Email"];
			if (reader["AuthInfo"] != null && reader["AuthInfo"] != DBNull.Value) obj.AuthInfo = (string)reader["AuthInfo"];
			if (reader["ConsentForPublishing"] != null && reader["ConsentForPublishing"] != DBNull.Value) obj.ConsentForPublishing = (bool)reader["ConsentForPublishing"];
			if (reader["NationalityCode"] != null && reader["NationalityCode"] != DBNull.Value) obj.NationalityCode = (string)reader["NationalityCode"];
			if (reader["EntityType"] != null && reader["EntityType"] != DBNull.Value) obj.EntityType = (int)reader["EntityType"];
			if (reader["EntityTypeDescription"] != null && reader["EntityTypeDescription"] != DBNull.Value) obj.EntityTypeDescription = (string)reader["EntityTypeDescription"];
			if (reader["RegCode"] != null && reader["RegCode"] != DBNull.Value) obj.RegCode = (string)reader["RegCode"];
		}
		private void SetContactDataObjectParams(ref SqlCommand command, ContactDataObject obj)
		{
			command.Parameters.Add("@InternalID", SqlDbType.Int);
			command.Parameters["@InternalID"].Direction = ParameterDirection.InputOutput;
			command.Parameters.Add("@OwnerID", SqlDbType.UniqueIdentifier).Value = obj.OwnerID;
			if (!string.IsNullOrEmpty(obj.ID)) command.Parameters.Add("@ID", SqlDbType.NVarChar, 32).Value = obj.ID;
			if (!string.IsNullOrEmpty(obj.Name)) command.Parameters.Add("@Name", SqlDbType.NVarChar, 255).Value = obj.Name;
			if (!string.IsNullOrEmpty(obj.Organization)) command.Parameters.Add("@Organization", SqlDbType.NVarChar, 255).Value = obj.Organization;
			if (!string.IsNullOrEmpty(obj.Street1)) command.Parameters.Add("@Street1", SqlDbType.NVarChar, 255).Value = obj.Street1;
			if (!string.IsNullOrEmpty(obj.Street2)) command.Parameters.Add("@Street2", SqlDbType.NVarChar, 255).Value = obj.Street2;
			if (!string.IsNullOrEmpty(obj.Street3)) command.Parameters.Add("@Street3", SqlDbType.NVarChar, 255).Value = obj.Street3;
			if (!string.IsNullOrEmpty(obj.City)) command.Parameters.Add("@City", SqlDbType.NVarChar, 255).Value = obj.City;
			if (!string.IsNullOrEmpty(obj.District)) command.Parameters.Add("@District", SqlDbType.NVarChar, 255).Value = obj.District;
			if (!string.IsNullOrEmpty(obj.PostalCode)) command.Parameters.Add("@PostalCode", SqlDbType.NVarChar, 16).Value = obj.PostalCode;
			if (!string.IsNullOrEmpty(obj.CountryCode)) command.Parameters.Add("@CountryCode", SqlDbType.NVarChar, 16).Value = obj.CountryCode;
			if (!string.IsNullOrEmpty(obj.PhonePrefix)) command.Parameters.Add("@PhonePrefix", SqlDbType.NVarChar, 16).Value = obj.PhonePrefix;
			if (!string.IsNullOrEmpty(obj.Phone)) command.Parameters.Add("@Phone", SqlDbType.NVarChar, 16).Value = obj.Phone;
			if (!string.IsNullOrEmpty(obj.PhoneInternal)) command.Parameters.Add("@PhoneInternal", SqlDbType.NVarChar, 16).Value = obj.PhoneInternal;
			if (!string.IsNullOrEmpty(obj.FaxPrefix)) command.Parameters.Add("@FaxPrefix", SqlDbType.NVarChar, 16).Value = obj.FaxInternal;
			if (!string.IsNullOrEmpty(obj.Fax)) command.Parameters.Add("@Fax", SqlDbType.NVarChar, 16).Value = obj.Fax;
			if (!string.IsNullOrEmpty(obj.FaxInternal)) command.Parameters.Add("@FaxInternal", SqlDbType.NVarChar, 16).Value = obj.FaxInternal;
			if (!string.IsNullOrEmpty(obj.Email)) command.Parameters.Add("@Email", SqlDbType.NVarChar, 64).Value = obj.Email;
			if (!string.IsNullOrEmpty(obj.AuthInfo)) command.Parameters.Add("@AuthInfo", SqlDbType.NVarChar, 255).Value = obj.AuthInfo;
			command.Parameters.Add("@ConsentForPublishing", SqlDbType.Bit).Value = obj.ConsentForPublishing;
			if (!string.IsNullOrEmpty(obj.NationalityCode)) command.Parameters.Add("@NationalityCode", SqlDbType.NVarChar, 16).Value = obj.NationalityCode;
			if (obj.EntityType > 0) command.Parameters.Add("@EntityType", SqlDbType.Int).Value = obj.EntityType;
			if (!string.IsNullOrEmpty(obj.RegCode)) command.Parameters.Add("@RegCode", SqlDbType.NVarChar, 128).Value = obj.RegCode;

			if (!string.IsNullOrEmpty(obj.NicID)) command.Parameters.Add("@NicID", SqlDbType.NVarChar, 128).Value = obj.NicID;
			if (!string.IsNullOrEmpty(obj.Status1)) command.Parameters.Add("@Status1", SqlDbType.NVarChar, 128).Value = obj.Status1;
			if (!string.IsNullOrEmpty(obj.Status2)) command.Parameters.Add("@Status2", SqlDbType.NVarChar, 128).Value = obj.Status2;
			if (!string.IsNullOrEmpty(obj.CurrentRegistrar)) command.Parameters.Add("@CurrentRegistrar", SqlDbType.NVarChar, 128).Value = obj.CurrentRegistrar;
			if (!string.IsNullOrEmpty(obj.CreateRegistrar)) command.Parameters.Add("@CreateRegistrar", SqlDbType.NVarChar, 128).Value = obj.CreateRegistrar;
			if (!string.IsNullOrEmpty(obj.UpdateRegistrar)) command.Parameters.Add("@UpdateRegistrar", SqlDbType.NVarChar, 128).Value = obj.UpdateRegistrar;
			if (obj.CreateDate.HasValue) command.Parameters.Add("@CreateDate", SqlDbType.DateTime).Value = obj.CreateDate;
			if (obj.UpdateDate.HasValue) command.Parameters.Add("@UpdateDate", SqlDbType.DateTime).Value = obj.UpdateDate;
		}
		private void ReadTransactionLogDataObject(ref SqlDataReader reader, ref TransactionLogDataObject obj)
		{
			if (reader["OwnerID"] != null && reader["OwnerID"] != DBNull.Value) obj.OwnerID = (Guid)reader["OwnerID"];
			if (reader["InternalID"] != null && reader["InternalID"] != DBNull.Value) obj.InternalID = (int)reader["InternalID"];
			if (reader["Session"] != null && reader["Session"] != DBNull.Value) obj.Session = (string)reader["Session"];
			if (reader["TargetUrl"] != null && reader["TargetUrl"] != DBNull.Value) obj.TargetUrl = (string)reader["TargetUrl"];
			if (reader["ServerTransactionID"] != null && reader["ServerTransactionID"] != DBNull.Value) obj.ServerTransactionID = (string)reader["ServerTransactionID"];
			if (reader["ClientTransactionID"] != null && reader["ClientTransactionID"] != DBNull.Value) obj.ClientTransactionID = (string)reader["ClientTransactionID"];
			if (reader["RequestRawData"] != null && reader["RequestRawData"] != DBNull.Value) obj.RequestRawData = (string)reader["RequestRawData"];
			if (reader["ResponseRawData"] != null && reader["ResponseRawData"] != DBNull.Value) obj.ResponseRawData = (string)reader["ResponseRawData"];
			if (reader["TransactionDate"] != null && reader["TransactionDate"] != DBNull.Value) obj.TransactionDate = (DateTime)reader["TransactionDate"];
		}
		private void SetTransactionLogDataObjectParams(ref SqlCommand command, TransactionLogDataObject obj)
		{
			if (!obj.OwnerID.Equals(Guid.Empty)) command.Parameters.Add("@OwnerID", SqlDbType.UniqueIdentifier).Value = obj.OwnerID;
			if (!string.IsNullOrEmpty(obj.Session)) command.Parameters.Add("@Session", SqlDbType.NVarChar, 255).Value = obj.Session;
			if (!string.IsNullOrEmpty(obj.TargetUrl)) command.Parameters.Add("@TargetUrl", SqlDbType.NVarChar, 255).Value = obj.TargetUrl;
			if (!string.IsNullOrEmpty(obj.ServerTransactionID)) command.Parameters.Add("@ServerTransactionID", SqlDbType.NVarChar, 128).Value = obj.ServerTransactionID;
			if (!string.IsNullOrEmpty(obj.ClientTransactionID)) command.Parameters.Add("@ClientTransactionID", SqlDbType.NVarChar, 128).Value = obj.ClientTransactionID;
			if (!string.IsNullOrEmpty(obj.RequestRawData)) command.Parameters.Add("@RequestRawData", SqlDbType.NVarChar, -1).Value = obj.RequestRawData;
			if (!string.IsNullOrEmpty(obj.ResponseRawData)) command.Parameters.Add("@ResponseRawData", SqlDbType.NVarChar, -1).Value = obj.ResponseRawData;
			command.Parameters.Add("@TransactionDate", SqlDbType.DateTime).Value = obj.TransactionDate;
		}
		private void ReadHostDataObject(ref SqlDataReader reader, ref HostDataObject obj)
		{
			if (reader["InternalID"] != null && reader["InternalID"] != DBNull.Value) obj.InternalID = (int)reader["InternalID"];
			if (reader["DomainName"] != null && reader["DomainName"] != DBNull.Value) obj.DomainName = (string)reader["DomainName"];
			if (reader["Name"] != null && reader["Name"] != DBNull.Value) obj.Name = (string)reader["Name"];
			if (reader["Address"] != null && reader["Address"] != DBNull.Value) obj.Address = (string)reader["Address"];
		}
		private void SetHostDataObjectParams(ref SqlCommand command, HostDataObject obj)
		{
			if (!string.IsNullOrEmpty(obj.DomainName)) command.Parameters.Add("@DomainName", SqlDbType.NVarChar, 255).Value = obj.DomainName;
			if (!string.IsNullOrEmpty(obj.Name)) command.Parameters.Add("@Name", SqlDbType.NVarChar, 128).Value = obj.Name;
			if (!string.IsNullOrEmpty(obj.Address)) command.Parameters.Add("@Address", SqlDbType.NVarChar, 128).Value = obj.Address;
		}
		private void ReadEntityTypeDataObject(ref SqlDataReader reader, ref EntityTypeDataObject obj)
		{
			if (reader["InternalID"] != null && reader["InternalID"] != DBNull.Value) obj.InternalID = (int)reader["InternalID"];
			if (reader["Description"] != null && reader["Description"] != DBNull.Value) obj.Description = (string)reader["Description"];
			if (reader["Value"] != null && reader["Value"] != DBNull.Value) obj.Value = (int)reader["Value"];
		}
		private void ReadCountryDataObject(ref SqlDataReader reader, ref CountryDataObject obj)
		{
			if (reader["Code"] != null && reader["Code"] != DBNull.Value) obj.Code = (string)reader["Code"];
			if (reader["Description"] != null && reader["Description"] != DBNull.Value) obj.Description = (string)reader["Description"];
			if (reader["PhonePrefix"] != null && reader["PhonePrefix"] != DBNull.Value) obj.PhonePrefix = (string)reader["PhonePrefix"];
		}
		private void ReadConfigurationDataObject(ref SqlDataReader reader, ref ConfigurationDataObject obj)
		{
			if (reader["UserID"] != null && reader["UserID"] != DBNull.Value) obj.UserID = (Guid)reader["UserID"];
			if (reader["Account"] != null && reader["Account"] != DBNull.Value) obj.Account = (string)reader["Account"];
			if (reader["Password"] != null && reader["Password"] != DBNull.Value) obj.Password = (string)reader["Password"];
			if (reader["TargetUrl"] != null && reader["TargetUrl"] != DBNull.Value) obj.TargetUrl = (string)reader["TargetUrl"];
			if (reader["TargetDeletedUrl"] != null && reader["TargetDeletedUrl"] != DBNull.Value) obj.TargetDeletedUrl = (string)reader["TargetDeletedUrl"];
			if (reader["OperationCount"] != null && reader["OperationCount"] != DBNull.Value) obj.OperationCount = (int)reader["OperationCount"];
			if (reader["IsTest"] != null && reader["IsTest"] != DBNull.Value) obj.IsTest = (bool)reader["IsTest"];
		}

        private void SetDomainDataObjectParams(ref SqlCommand command, DomainDataObject obj)
        {
            command.Parameters.Add("@InternalID", SqlDbType.Int);
            command.Parameters["@InternalID"].Direction = ParameterDirection.InputOutput;
            command.Parameters.Add("@OwnerID", SqlDbType.UniqueIdentifier).Value = obj.OwnerID;
            if (!string.IsNullOrEmpty(obj.ROID)) command.Parameters.Add("@ROID", SqlDbType.NVarChar, 255).Value = obj.ROID;
            if (!string.IsNullOrEmpty(obj.Name)) command.Parameters.Add("@Name", SqlDbType.NVarChar, 255).Value = obj.Name;
            if (!string.IsNullOrEmpty(obj.CurrentRegistrar)) command.Parameters.Add("@CurrentRegistrar", SqlDbType.NVarChar, 255).Value = obj.CurrentRegistrar;
            if (!string.IsNullOrEmpty(obj.UpdateRegistrar)) command.Parameters.Add("@UpdateRegistrar", SqlDbType.NVarChar, 255).Value = obj.UpdateRegistrar;
            if (obj.CreateDate.HasValue) command.Parameters.Add("@CreateDate", SqlDbType.DateTime).Value = obj.CreateDate.Value;
            if (obj.UpdateDate.HasValue) command.Parameters.Add("@UpdateDate", SqlDbType.DateTime).Value = obj.UpdateDate.Value;
            if (obj.TransferDate.HasValue) command.Parameters.Add("@TransferDate", SqlDbType.DateTime).Value = obj.TransferDate.Value;
            if (obj.DeleteDate.HasValue) command.Parameters.Add("@DeleteDate", SqlDbType.DateTime).Value = obj.DeleteDate.Value;
            if (!string.IsNullOrEmpty(obj.Registrant)) command.Parameters.Add("@Registrant", SqlDbType.NVarChar, 255).Value = obj.Registrant;
            if (!string.IsNullOrEmpty(obj.Tech)) command.Parameters.Add("@Tech", SqlDbType.NVarChar, 255).Value = obj.Tech;
            if (!string.IsNullOrEmpty(obj.Admin)) command.Parameters.Add("@Admin", SqlDbType.NVarChar, 255).Value = obj.Admin;
            if (!string.IsNullOrEmpty(obj.Status)) command.Parameters.Add("@Status", SqlDbType.NVarChar, 255).Value = obj.Status;
            if (!string.IsNullOrEmpty(obj.AuthInfo)) command.Parameters.Add("@AuthInfo", SqlDbType.NVarChar, 255).Value = obj.AuthInfo;
        }
        private void ReadDomainDataObject(ref SqlDataReader reader, ref DomainDataObject obj)
        {
            if (reader["InternalID"] != null && reader["InternalID"] != DBNull.Value) obj.InternalID = (int)reader["InternalID"];
            if (reader["ROID"] != null && reader["ROID"] != DBNull.Value) obj.ROID = (string)reader["ROID"];
            if (reader["Name"] != null && reader["Name"] != DBNull.Value) obj.Name = (string)reader["Name"];
            if (reader["CurrentRegistrar"] != null && reader["CurrentRegistrar"] != DBNull.Value) obj.CurrentRegistrar = (string)reader["CurrentRegistrar"];
            if (reader["UpdateRegistrar"] != null && reader["UpdateRegistrar"] != DBNull.Value) obj.UpdateRegistrar = (string)reader["UpdateRegistrar"];
            if (reader["CreateDate"] != null && reader["CreateDate"] != DBNull.Value) obj.CreateDate = (DateTime?)reader["CreateDate"];
            if (reader["UpdateDate"] != null && reader["UpdateDate"] != DBNull.Value) obj.UpdateDate = (DateTime?)reader["UpdateDate"];
            if (reader["DeleteDate"] != null && reader["DeleteDate"] != DBNull.Value) obj.DeleteDate = (DateTime?)reader["DeleteDate"];
            if (reader["ExpireDate"] != null && reader["ExpireDate"] != DBNull.Value) obj.ExpireDate = (DateTime?)reader["ExpireDate"];
            if (reader["TransferDate"] != null && reader["TransferDate"] != DBNull.Value) obj.TransferDate = (DateTime?)reader["TransferDate"];
            if (reader["Status"] != null && reader["Status"] != DBNull.Value) obj.Status = (string)reader["Status"];
            if (reader["AuthInfo"] != null && reader["AuthInfo"] != DBNull.Value) obj.AuthInfo = (string)reader["AuthInfo"];
            if (reader["Registrant"] != null && reader["Registrant"] != DBNull.Value) obj.AuthInfo = (string)reader["Registrant"];
            if (reader["Tech"] != null && reader["Tech"] != DBNull.Value) obj.AuthInfo = (string)reader["Tech"];
            if (reader["Admin"] != null && reader["Admin"] != DBNull.Value) obj.AuthInfo = (string)reader["Admin"];
        }
		#endregion
	}
}